import CURDHelper from '@/utils/CURDHelper'
import PageConfig from '@/utils/pageConfig'
import { getBtnCtr  } from '@/utils/BtnControl'
import  Query from '@/components/Query';
import { isNumber} from '@/utils/validate';
import { VxeUI } from 'vxe-pc-ui';
import XEUtils from 'xe-utils';
export default {
    name:'CustomerFollowUp',
    components: {
       Query 
    },
    data() {
      const gltxItemRender = {
        name: 'VxeSelect',
        options: []
      }
      return {
        gltxItemRender,
        componentSize:PageConfig.ComponentSize,
        duration:PageConfig.Duration,
        btnPromission: '',
        editDrawer:false,
        BaseApiUrl:'/api/Xmjckhxx',
        BaseChildApiUrl:'/api/Xmkhgdgl',
        EditRow:undefined,
        CurrCustomerName:'',
        QueryConditions:[],
        QueryConfig:[{
            title:'客户名称',
            field:'khmc',
            type:'input',
            value:'',
            placeholder:'请输入客户名称以查询',
          }
        ],
        GridOptions:{
          id:'CustomerFollowUpMainGrid',
          border: true,
          stripe: true,
          loading: false,
          size:PageConfig.ComponentSize,
          height: 700,
          columnConfig: {
            resizable: true
          },
          rowConfig: {
            isHover: true,
            isCurrent:true
          },
          checkboxConfig: {
            highlight: true,
            range: true
          },
          exportConfig: {
          },
          toolbarConfig: {
              custom:true
          },
          customConfig:{
            mode:'drawer',
            storage:true
          },
          columns: [
            { type: 'seq', width: 50 },
            { type: 'checkbox', width: 35 },
            { field: 'khbm', title: '客户编码', sortable: true ,minWidth: 100 ,showOverflow: true},
            { field: 'khmc', title: '客户名称', sortable: true,minWidth: 150 ,showOverflow: true},
            { field: 'khjc', title: '客户简称', sortable: true,minWidth: 100 ,showOverflow: true},
            { field: 'kyrq', title: '开业日期', sortable: true,minWidth: 120 ,showOverflow: true,
              formatter ({ cellValue }) {
                return XEUtils.toDateString(cellValue, 'yyyy-MM-dd');
              }
            },
            { field: 'zczj', title: '注册资金', minWidth: 80 },
            { field: 'ygrs', title: '员工人数', minWidth: 80 },
            { field: 'lxr', title: '联系人', minWidth: 80 },
            { field: 'lxdh', title: '联系电话', minWidth: 80 },
            { field: 'gsdz', title: '公司地址', minWidth: 100 ,showOverflow: true},
            { field: 'shdz', title: '收货地址',minWidth:100,showOverflow: true},
            { field: 'khly', title: '客户来源',minWidth:100,showOverflow: true},
            { field: 'khzt', title: '状态',minWidth:100,showOverflow: true},
            { field: 'bz', title: '备注', minWidth: 100,showOverflow: true},
            { field: 'createtime', title: '创建时间', minWidth: 150,showOverflow: true},
            { field: 'createname', title: '创建人', minWidth: 100,showOverflow: true},
            { field: 'updatetime', title: '更新时间', minWidth: 150,showOverflow: true},
            { field: 'updatename', title: '更新人', minWidth: 100,showOverflow: true},
            { field: 'operation' , title:'操作',fixed: 'right', width:60,slots:{default:'operation'}},
          ],
          data: []
        },
        PagerConfig:{
          align:'center',
          pageSize: 25,
          total: 0,
          currentPage: 1, 
          pageSizes: [10,25, 50, 100, 200],
        },
        FormOptions:{
          'title-width':90,
          border:false,
          titleBackground:false,
          data:{
            id:-1,
            khid:0,
            gdr:undefined,
            gjsj:undefined,
            gdxq:undefined,
            xcgjsj:undefined,
            txid:undefined,
            txmc:undefined
          },
          rules:{            
            gjsj:[{required:true,message:'请选择跟进时间',trigger:'blur'}],
            gdxq:[{required:true,message:'请输入跟进详情',trigger:'blur'}],
            txid:[{required:true,message:'请选择所跟进的体系',trigger:'blur'}]
          },
          items:[
            {field:'gjsj',title:'跟进时间',span:12,itemRender:{name:'VxeDatePicker',type:'date'}},             
            {field:'xcgjsj',title:'下次跟进',span:12,itemRender:{name:'VxeDatePicker',type:'date'}},
            {field:'txid',title:'体系',span:12,itemRender:gltxItemRender},
            {field:'gdxq',title:'详情',span:24,itemRender:{name:'VxeTextarea',maxlegnth:500,clearable:true,showWordCount:true},placeholder:'请输入跟进详情'},
            {
              align:'right',
              span:24,
              itemRender:{
                name:'VxeButtonGroup',
                options:[{
                  type:'submit',content:'提交',status:'primary'                
                }]
              }
            }
          ]
        },
        FollowUpList:[]
      }
    },
    created() {
      // 动态头部按钮
      this.btnPromission = getBtnCtr(
        this.$route.path,
        this.$store.getters.userPermission
      )
      this.GetTxOptions();
    },
    methods: {
      ValidatorNumber(e){
         if(!isNumber(e.itemValue)){
             return new Error('请输入数字');
         }else{
           if(e.itemValue<0){
             return new Error('请输入大于等于0的数字');
           }
         }
      },
      FormatDate(value){
        return XEUtils.toDateString(value,'MM-dd');
      },
      handleQuery(e) {
        this.QueryConditions = e;
        this.DoQuery(e);
      },
      handlePageChange(){
        this.DoQuery(this.QueryConditions);
      },
      handleFollowUp(row) {
        this.FormOptions.data = {
          id:-1,
          khid:row.id,
          gdr:undefined,
          gjsj:undefined,
          gdxq:undefined,
          xcgjsj:undefined,
          txid:undefined,
          txmc:undefined
        };
        this.CurrCustomerName = row.khmc;
        this.EditRow = row;
        this.DoChildQuery(row.id)
        this.editDrawer = true;
      },
      handleEditDrawerSubmit(){
          this.DoChildAdd();
      },
      handleEditModalReset(){
          
      },
      handleDelete(item){
        this.DoChildDelete(item);
      },
      DoQuery(e){
        let request = {
          isPaging: true,
          pageSize:this.PagerConfig.pageSize,
          pageIndex:this.PagerConfig.currentPage,
          queryConditions:e,
          orderByConditions:[]
        }
        CURDHelper.Query(request,this.BaseApiUrl)
          .then(res=>{
            this.GridOptions.data = res.data;
            this.PagerConfig.total = res.totalNumber;
          }).catch(err=>{
              VxeUI.modal.message({content: err,status:'error',duration: this.duration})
          })
      },
      DoChildAdd(){
        //新增
        this.gltxItemRender.options.forEach(item=>{
          if(item.value == this.FormOptions.data.txid){
            this.FormOptions.data.txmc = item.label;
          }
        })
        CURDHelper.Add(this.FormOptions.data,null,this.BaseChildApiUrl)
        .then(res=>{
           VxeUI.modal.message({content: '操作成功',status: 'success',duration: this.duration})
           this.$refs.editForm.reset();           
           this.FormOptions.data = {
            id:-1,
            khid:this.EditRow.id,
            gdr:undefined,
            gjsj:undefined,
            gdxq:undefined,
            xcgjsj:undefined,
            txid:0,
            txmc:''
          };
          this.DoChildQuery(this.EditRow.id);          
        }).catch(err=>{
          VxeUI.modal.message({content: err,status:'error',duration: this.duration})
        })
      },      
      DoChildDelete(row){
        //删除
        let deleteModel = {
          ID:row.id,
          DeleteRemark:''
        };
        CURDHelper.DeleteWithConfirm('是否删除当前数据',deleteModel,null,this.BaseChildApiUrl)
        .then(res=>{
          VxeUI.modal.message({content: '删除数据成功',status: 'success',duration: this.duration})
          for(let i=this.FollowUpList.length-1;i>=0;i--){
            if(this.FollowUpList[i].id == row.id){
              this.FollowUpList.splice(i,1);
            }
          }
        }).catch(err=>{
          VxeUI.modal.message({content: err,status:'error',duration: this.duration})
        })
      },
      DoChildQuery(id){
        let request = {
          isPaging: true,
          pageSize:this.PagerConfig.pageSize,
          pageIndex:this.PagerConfig.currentPage,
          queryConditions:[{column:'khid',content:id,condition:'0'}],
          orderByConditions:[{column:'gjsj',condition:'desc'}]
        }
        CURDHelper.Query(request,this.BaseChildApiUrl)
          .then(res=>{
            this.FollowUpList = res.data;
          }).catch(err=>{
              VxeUI.modal.message({content: err,status:'error',duration: this.duration})
          })
      },
      GetTxOptions() {
        let request = {
          isPaging: false,
          pageSize: 1,
          pageIndex: 1,
          queryConditions: [],
          orderByConditions: []
        }
        CURDHelper.Query(request, '/api/Xmjcgltx')
          .then(res => {
            this.gltxItemRender.options = [];
            res.data.forEach(item => {
              this.gltxItemRender.options.push({
                value: item.id,
                label: item.txmc
              })
            })
          }).catch(err => {
            VxeUI.modal.message({content: `${err}`,status: 'error',duration: this.duration})
          })
      },
    }
  }
